﻿using System;
using System.Collections.Generic;

using System.Text;

namespace RoughJs.Ast
{
    public class WrapFactory
    {

        public Object wrap(Context cx, Scriptable scope,
                       Object obj, Type staticType)
        {
            if (obj == null || obj == Undefined.instance
                || obj is Scriptable)
            {
                return obj;
            }
            //if (staticType != null && staticType.isPrimitive()) {
            //    if (staticType == Void.TYPE)
            //        return Undefined.instance;
            //    if (staticType == Character.TYPE)
            //        return new Integer(((Character) obj).charValue());
            //    return obj;
            //}
            if (!isJavaPrimitiveWrap())
            {
                if (obj is String || obj is double
                    || obj is Boolean)
                {
                    return obj;
                }
                //} else if (obj is Character) {
                //    return String.valueOf(((Character)obj).charValue());
                //}
            }
            Type cls = obj.GetType();
            if (cls.IsArray)
            {
                return NativeJavaArray.wrap(scope, obj);
            }
            return wrapAsJavaObject(cx, scope, obj, staticType);
        }
        public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
                                       Object javaObject, Type staticType)
        {
            Scriptable wrap;
            wrap = new NativeJavaObject(scope, javaObject, staticType);
            return wrap;
        }

        /**
    * @see #isJavaPrimitiveWrap()
    */
        public void setJavaPrimitiveWrap(bool value)
        {
            Context cx = Context.getCurrentContext();
            if (cx != null && cx.isSealed())
            {
                Context.onSealedMutation();
            }
            javaPrimitiveWrap = value;
        }
        public bool isJavaPrimitiveWrap()
        {
            return javaPrimitiveWrap;
        }
        private bool javaPrimitiveWrap = true;
    }//end for class
}
